package com.flyco.modules.stm.service.impl;

import com.flyco.modules.stm.entity.StmPrintTemplate;
import com.flyco.modules.stm.entity.StmPrintContent;
import com.flyco.modules.stm.mapper.StmPrintContentMapper;
import com.flyco.modules.stm.mapper.StmPrintTemplateMapper;
import com.flyco.modules.stm.service.IStmPrintTemplateService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 打印模板
 * @Author: flyco
 * @Date:   2020-10-27
 * @Version: V1.0
 */
@Service
public class StmPrintTemplateServiceImpl extends ServiceImpl<StmPrintTemplateMapper, StmPrintTemplate> implements IStmPrintTemplateService {

	@Autowired
	private StmPrintTemplateMapper stmPrintTemplateMapper;
	@Autowired
	private StmPrintContentMapper stmPrintContentMapper;
	
	@Override
	@Transactional
	public void saveMain(StmPrintTemplate stmPrintTemplate, List<StmPrintContent> stmPrintContentList) {
		stmPrintTemplateMapper.insert(stmPrintTemplate);
		for(StmPrintContent entity:stmPrintContentList) {
			//外键设置
			entity.setTemplateCode(stmPrintTemplate.getCode());
			stmPrintContentMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(StmPrintTemplate stmPrintTemplate,List<StmPrintContent> stmPrintContentList) {
		stmPrintTemplateMapper.updateById(stmPrintTemplate);
		
		//1.先删除子表数据
		stmPrintContentMapper.deleteByMainId(stmPrintTemplate.getCode());
		
		//2.子表数据重新插入
		for(StmPrintContent entity:stmPrintContentList) {
			//外键设置
			entity.setTemplateCode(stmPrintTemplate.getCode());
			stmPrintContentMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		stmPrintContentMapper.deleteByMainId(id);
		stmPrintTemplateMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			stmPrintContentMapper.deleteByMainId(id.toString());
			stmPrintTemplateMapper.deleteById(id);
		}
	}
	
}
